package com.gm.census.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gm.census.annotations.validation.OneOf;
import com.gm.census.model.UserInfo;
import com.gm.census.model.pojo.Ask;
import com.gm.census.model.pojo.Entry;
import com.gm.census.service.EntrySearchResult;
import com.gm.census.service.EntryService;
import com.gm.census.service.PagedListResult;
import com.gm.census.service.ServiceFactory;
import com.gm.census.service.UserInfoFacade;

@Controller
@RequestMapping("/census")
public class MainController extends BaseController {

	public MainController(ServiceFactory fact, UserInfoFacade userInfo) {
		super(fact, userInfo);
	}

	@RequestMapping(method = RequestMethod.GET)
	public String login(HttpServletRequest req, Model m) {
		String id = userInfo().getLoggedUserId();
		UserInfo usr = services().getUserService().find(id);
		if (usr == null) {
			Ask ask = services().getAskService().findByEmail(id);
			m.addAttribute("ask",ask);
			return "validate";
		} else {
			m.addAttribute("user", usr);
			return "main";
		}
	}

	@RequestMapping(value = "/ask", method = RequestMethod.PUT)
	public String ask(Ask a, Model m) {
		String loggedId = userInfo().getLoggedUserId();
		Ask saved = services().getAskService().findByEmail(loggedId);
		if (saved != null) {
			m.addAttribute("ask", saved);
			return "alreadyAsked";
		} else {
			a.setEmail(loggedId);
			m.addAttribute("ask", services().getAskService().put(a));
			return "askSuccess";
		}
	}

	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String search(Model m,
			@RequestParam(value = "q", required = false) String q,
			@RequestParam(value = "np", required = false) String np) {
		PagedListResult<List<Entry>> entries;
		if (q == null || q.length() == 0) {
			entries = services().getIndicationService().list(null);
			m.addAttribute("icp", entries.getCurrentPageIndex());
			m.addAttribute("inp", entries.getNextPageIndex());
			m.addAttribute("indications", entries.getReturn());

			entries = services().getVisitService().list(null);
			m.addAttribute("vcp", entries.getCurrentPageIndex());
			m.addAttribute("vnp", entries.getNextPageIndex());
			m.addAttribute("visits", entries.getReturn());
		} else {
			EntrySearchResult<Entry> res = services().getIndicationService()
					.search(q, np, null);
			m.addAttribute("cp", res.getCurrentPageIndex());
			m.addAttribute("np", res.getNextPageIndex());
			m.addAttribute("indications", res.getIndications());
			m.addAttribute("visits", res.getVisits());
		}
		return "search";
	}

	@RequestMapping(value = "/entry", method = RequestMethod.GET)
	public String newRegister(Entry e, Model m) {
		return "entry/add";
	}

	@RequestMapping(value = "/indication/{id}", method = RequestMethod.GET)
	public String indication(Model m, @PathVariable("id") Long id) {
		m.addAttribute("indication", services().getIndicationService().get(id));
		return "entry";
	}

	@RequestMapping(value = "/indications", method = RequestMethod.GET)
	public String indications(Model m, @RequestParam("np") String np) {
		PagedListResult<List<Entry>> entries = services()
				.getIndicationService().list(np);
		m.addAttribute("cp", entries.getCurrentPageIndex());
		m.addAttribute("np", entries.getNextPageIndex());
		m.addAttribute("items", entries.getReturn());
		return "indications";
	}

	@RequestMapping(value = "/visit/{id}", method = RequestMethod.GET)
	public String getVisit(Model m, @PathVariable("id") Long id) {
		m.addAttribute("visit", services().getVisitService().get(id));
		return "entry";
	}

	@RequestMapping(value = "/visits", method = RequestMethod.GET)
	public String visits(Model m, @RequestParam("np") String np) {
		PagedListResult<List<Entry>> entries = services().getVisitService()
				.list(np);
		m.addAttribute("cp", entries.getCurrentPageIndex());
		m.addAttribute("np", entries.getNextPageIndex());
		m.addAttribute("items", entries.getReturn());
		return "visits";
	}

	@RequestMapping(value = "/entry", method = RequestMethod.PUT)
	
	public String add(@Valid Entry e, BindingResult bind, Model m) {
		EntryService srv = e.getConfirmed() != null && e.getConfirmed() ? services()
				.getVisitService() : services().getIndicationService();
		srv.put(e);
		return "entry";
	}
}
